home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.11 Nov 91 / Icon List source / CListObj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-05  |  6.7 KB  |  269 lines  |  [TEXT/KAHL]

  1. /*    ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  2.     Object List
  3.  
  4.     Defines an object that holds a list of other objects and uses the 
  5.     List Manager to display them.
  6.     |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */
  7.  
  8. #ifndef         CLASS_LISTOBJ
  9. #include    "CListObj.h"
  10. #endif
  11.  
  12. /* |||||||||||||||||||| */
  13.  void    CListObj::IList ( Rect *viewR, int cellHeight, Boolean hasScrollBar, 
  14.                                  int selectMethod, WindowPtr theWin )
  15.     {
  16.     Rect                    dataBounds, viewBounds;
  17.     Point                    cellSize;
  18.     Handle                fakeDefProc;
  19.     int                    jmpInstruction;
  20.     long                    jmpAddress;
  21.     
  22.     /*** create the "fake" defproc handle and put a jump to our code in it */
  23.     jmpInstruction = 0x4EF9;
  24.     jmpAddress = (long) ListFunction;
  25.     fakeDefProc = GetResource( 'LDEF', MYPROCID );
  26.     HLock ( fakeDefProc );
  27.     BlockMove ( &jmpInstruction, *fakeDefProc, sizeof ( int ) );
  28.     BlockMove ( &jmpAddress, *fakeDefProc + 2, sizeof ( long ) );
  29.     HUnlock ( fakeDefProc );
  30.  
  31.     /*** initialize the list object, set up the corresponding list manager object */
  32.     theList = NULL;
  33.     viewRect = viewBounds = *viewR;
  34.     drawIt = true;
  35.     SetRect ( &dataBounds, 0, 0, 1, 0 );
  36.     cellSize.h = viewR->right - viewR->left;
  37.     cellSize.v = cellHeight;
  38.     viewBounds.right -= 15;
  39.     theList = LNew ( &viewBounds, &dataBounds, cellSize, MYPROCID, theWin, true, false, false, hasScrollBar );
  40.     (*theList)->selFlags = (SignedByte) selectMethod;
  41.     (*theList)->refCon = (long) this;
  42.     }
  43.  
  44. /* |||||||||||||||||||| */
  45.  void    CListObj::Dispose ( void )
  46.     {
  47.     /*** dispose of the list, but not it's contents */
  48.     LDispose ( theList );
  49.     inherited::Dispose ( );
  50.     }
  51.  
  52. /* |||||||||||||||||||| */
  53.  Boolean    CListObj::MouseInList ( Point thePoint, int modifiers )
  54.     {
  55.     /*** process a mouse click in the list by calling the toolbox List Manager */
  56.     if ( PtInRect ( thePoint, &viewRect ) && drawIt ) {
  57.         LClick ( thePoint, modifiers, theList );
  58.         return true;            /* return true if the mouse point WAS in the list */
  59.         }
  60.     return false;                /* return false if it was a miss */
  61.     }
  62.  
  63. /* |||||||||||||||||||| */
  64.  void    CListObj::UpdateList ( RgnHandle updateRgn )
  65.     {
  66.     /*** process an update event (only if drawing is turned on) */
  67.     if ( drawIt ) LUpdate ( updateRgn, theList );
  68.     }
  69.  
  70. /* |||||||||||||||||||| */
  71.  void    CListObj::ActivateList ( Boolean activate )
  72.     {
  73.     /*** process an activate event */
  74.     LActivate ( activate, theList );    
  75.     }
  76.  
  77. /* |||||||||||||||||||| */
  78.  void    CListObj::DrawingOn ( Boolean drawing )
  79.     {
  80.     /*** turn drawing on or off */
  81.     drawIt = drawing;
  82.     LDoDraw ( drawing, theList );
  83.     }
  84.  
  85. /* |||||||||||||||||||| */
  86.  void    CListObj::Scroll ( int amount )
  87.     {
  88.     /*** scroll the list (positive numbers scroll up) */
  89.     LScroll ( 0, amount, theList );
  90.     }
  91.  
  92. /* |||||||||||||||||||| */
  93.  int CListObj::NumObjs ( void )
  94.     {
  95.     /*** return the number of objects in the list */
  96.     return ( (*theList)->dataBounds.bottom );
  97.     }
  98.  
  99. /* |||||||||||||||||||| */
  100. void CListObj::Add ( CItemObj *theObject )
  101.     {
  102.     int    rowNum;
  103.     long    value;
  104.     Cell    theCell;
  105.     
  106.     /*** add an item to the bottom of the list */
  107.     rowNum = LAddRow ( 1, 32760, theList );
  108.     value = (long) theObject;
  109.     theCell.h = 0;
  110.     theCell.v = rowNum;
  111.     LSetCell ( &value, sizeof ( long ), theCell, theList );
  112.     }
  113.  
  114. /* |||||||||||||||||||| */
  115. void CListObj::Remove ( CItemObj *theObject )
  116.     {
  117.     int    nRows, len;
  118.     long    value;
  119.     Cell    theCell;
  120.     
  121.     /*** delete the object from the list, if it's in it (doesn't delete the object) */
  122.     len = sizeof ( long );
  123.     nRows = NumObjs ( );
  124.     for ( theCell.h = 0, theCell.v = 0; theCell.v < nRows; ++theCell.v ) {
  125.         LGetCell ( &value, &len, theCell, theList );
  126.         if ( value == (long) theObject ) {
  127.             LDelRow ( 1, theCell.v, theList );
  128.             return;
  129.             }
  130.         }
  131.     }
  132.  
  133. /* |||||||||||||||||||| */
  134. void CListObj::ForEach ( VoidFunc doThis )
  135.     {
  136.     int            nRows, len;
  137.     long            value;
  138.     Cell            theCell;
  139.     CItemObj    *theObject;
  140.     
  141.     /*** call the doThis function, passing each object in the list */
  142.     len = sizeof ( long );
  143.     nRows = NumObjs ( );
  144.     for ( theCell.h = 0, theCell.v = 0; theCell.v < nRows; ++theCell.v ) {
  145.         LGetCell ( &value, &len, theCell, theList );
  146.         theObject = (CItemObj *) value;
  147.         doThis ( theObject );
  148.         }
  149.     }
  150.  
  151. /* |||||||||||||||||||| */
  152. void CListObj::ForEachSelected ( VoidFunc doThis )
  153.     {
  154.     int            nRows, len;
  155.     long            value;
  156.     Cell            theCell;
  157.     CItemObj    *theObject;
  158.     
  159.     /*** call the VoidFunc, passing each selected object in the list */
  160.     len = sizeof ( long );
  161.     nRows = NumObjs ( );
  162.     theCell.h = theCell.v = 0;
  163.     while ( LGetSelect ( true, &theCell, theList ) ) {
  164.         LGetCell ( &value, &len, theCell, theList );
  165.         theObject = (CItemObj *) value;
  166.         doThis ( theObject );
  167.         ++theCell.v;
  168.         }
  169.     }
  170.  
  171. /* |||||||||||||||||||| */
  172. void CListObj::SelectNone ( void )
  173.     {
  174.     int            nRows;
  175.     long            value;
  176.     Cell            theCell;
  177.     CItemObj    *theObject;
  178.     
  179.     /*** de-select all items */
  180.     nRows = NumObjs ( );
  181.     theCell.h = theCell.v = 0;
  182.     while ( LGetSelect ( true, &theCell, theList ) ) {
  183.         LSetSelect ( false, theCell, theList );
  184.         ++theCell.v;
  185.         }
  186.     }
  187.  
  188. /* |||||||||||||||||||| */
  189. Boolean CListObj::AnySelected ( void )
  190.     {
  191.     Cell            theCell;
  192.     
  193.     /*** return true if any item is selected */
  194.     theCell.h = theCell.v = 0;
  195.     return ( LGetSelect ( true, &theCell, theList ) );
  196.     }
  197.  
  198. /* |||||||||||||||||||| */
  199. CItemObj * CListObj::FirstThat ( BooleanFunc test )
  200.     {
  201.     int            nRows, len;
  202.     long            value;
  203.     Cell            theCell;
  204.     CItemObj    *theObject;
  205.     
  206.     /*** return the first item that passes the test in function test */
  207.     len = sizeof ( long );
  208.     nRows = NumObjs ( );
  209.     for ( theCell.h = 0, theCell.v = 0; theCell.v < nRows; ++theCell.v ) {
  210.         LGetCell ( &value, &len, theCell, theList );
  211.         theObject = (CItemObj *) value;
  212.         if ( test ( theObject ) ) return ( theObject );
  213.         }
  214.     return ( NULL );
  215.     }
  216.  
  217. /* |||||||||||||||||||| */
  218. CItemObj * CListObj::GetIndObject ( int n )
  219.     {
  220.     int            len;
  221.     long            value;
  222.     Cell            theCell;
  223.     CItemObj    *theObject;
  224.  
  225.     /*** return the nth object in the list (list manager is 0 based, ListObj is 1 based) */
  226.     --n;
  227.     if ( (n < NumObjs ( )) && (n >= 0) ) {
  228.         theCell.h = 0;
  229.         theCell.v = n;
  230.         len = sizeof ( long );
  231.         LGetCell ( &value, &len, theCell, theList );
  232.         theObject = (CItemObj *) value;
  233.         return ( theObject );
  234.         }
  235.     return ( NULL );
  236.     }
  237.  
  238. /*    ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
  239. pascal void ListFunction ( int lMessage, Boolean lSelect, Rect *lRect, Cell lCell,
  240.                                     int lDataOffset, int lDataLen, ListHandle lHandle )
  241.     {
  242.     CItemObj    *item;
  243.     long            *theData;
  244.     
  245.     /*** dispatch a message sent to a list by the list manager to the object it goes with */
  246.     switch ( lMessage ) {
  247.         case lInitMsg:
  248.             break;
  249.         case lDrawMsg:
  250.             if ( lDataLen > 0 ) {
  251.                 theData = (long *) **(*lHandle)->cells;
  252.                 theData += lDataOffset/4;
  253.                 item = (CItemObj *) *theData;
  254.                 item->DrawItem ( lSelect, lRect );
  255.                 }
  256.             break;
  257.         case lHiliteMsg:
  258.             if ( lDataLen > 0 ) {
  259.                 theData = (long *) **(*lHandle)->cells;
  260.                 theData += lDataOffset/4;
  261.                 item = (CItemObj *) *theData;
  262.                 item->DrawItem ( lSelect, lRect );
  263.                 }
  264.             break;
  265.         case lCloseMsg:
  266.             break;
  267.         }
  268.     }
  269.